package com.origin.app

import android.app.Activity
import android.app.Application
import android.os.Bundle
import android.os.Looper
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.origin.greendaolibrary.greendao.BDao
import com.origin.netlibrary.RetrofitManager
import com.origin.netlibrary.netapi.RequestFormat
import com.origin.utils.CrashHandler
import com.origin.utils.log.LogUtils
import org.greenrobot.greendao.AbstractDao
import java.lang.ref.WeakReference
import java.util.logging.Handler

class MyApplication : Application() {


    companion object {

        private lateinit var application: MyApplication
        fun getInstance(): MyApplication {
            return application;
        }
        private val retrofitManager: RetrofitManager by lazy {
            RetrofitManager.getInstance(
                getInstance()
            )
        }

        val restService: RequestFormat by lazy {
            retrofitManager.requestFormat
        }
    }

    public val retrofitManager: RetrofitManager by lazy {
        RetrofitManager.getInstance(
            this
        )
    }
    private var mCurrentActivity: WeakReference<AppCompatActivity>? = null

    private var mMainActivity: WeakReference<AppCompatActivity>? = null

    public fun getCurrentActivity(): AppCompatActivity? {
        Log.e("获取当前","1")
        val activity = mCurrentActivity?.get()
        if (activity != null) {
            return activity
        }
        return mMainActivity?.get()
    }


    override fun onCreate() {

        super.onCreate()
        application = this;
        LogUtils.logFileUtilsInit()
//        ScreenAdaptationUtil.adjustDensity(this)
        CrashHandler.instance.init(applicationContext)
        LogUtils.__e("sad===>")
        Log.e("MyApplication===>","asd")
        val daoClasses =
            arrayOf<Class<out AbstractDao<*, *>>>(BDao::class.java)


        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {

            }

            override fun onActivityStarted(activity: Activity) {


            }

            override fun onActivityResumed(activity: Activity) {
                if (activity is AppCompatActivity) {
                    if (activity is MainActivity) {
                        LogUtils.__e("onActivityResumed  onResume")
                    }
                    mCurrentActivity = WeakReference(activity)

//                    if (activity is com.example.droidplugindemo.page.main.MainActivity) {
//                        mMainActivity = WeakReference(activity)
//                    }
                }
            }

            override fun onActivityPaused(activity: Activity) {
            }

            override fun onActivityStopped(activity: Activity) {

            }

            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
            }

            override fun onActivityDestroyed(activity: Activity) {
            }

        })
    }

}